<?php

namespace App\Http\Admin\Services\Scm;

use App\Exceptions\InvalidRequestException;
use App\Http\Models\ScmHasShops;
use App\Lib\PHPExcel\ExcelHandle;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Services\BaseService;

class ScmService  extends BaseService
{

    /*
     * 列表
     * */
    public function list($attrs =   []){

        $modal  =   ScmHasShops::query();
        if(!empty($attrs['scm_name'])){
            $modal  =   $modal->where('scm_name','like',"%".$attrs['scm_name']."%");
        }
        if(!empty($attrs['shop_name'])){
            $modal  =   $modal->where('shop_name','like',"%".$attrs['shop_name']."%");
        }
        $modal  =   $modal->paginate(50);

        return $modal;

    }

    /*
     * 添加/编辑
     * */

    public function addScm($attrs   =   []){

        if(empty($attrs['shop_name']) || empty($attrs['scm_name'])){
            throw new InvalidRequestException("供应链名称或店铺名为空");
        }
        unset($attrs['token']);
        if(empty($attrs['id'])){
            ScmHasShops::create($attrs);
        }
        if(!empty($attrs['id'])){
            ScmHasShops::where('id',$attrs['id'])->update($attrs);
        }

        return;

    }

    /*
     * 删除
     * */

    public function delete($attrs   =   []){

        if(empty($attrs['id'])){
            throw new InvalidRequestException("参数缺失");
        }
        ScmHasShops::where('id',$attrs['id'])->delete();

        return '删除成功!';

    }

}